package com.didispace.chapter11.service

import com.didispace.chapter11.mapper.UserMapper
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.jdbc.core.JdbcTemplate
import org.springframework.stereotype.Service


/**
 * @author 田桂森 2020/7/24 0024
 */
@Service
class IUserServiceImp : IUserService {
    lateinit var jdbcTemplate: JdbcTemplate

    @Autowired()
    lateinit var userMapper: UserMapper

    fun UserServiceImpl(jdbcTemplate: JdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate
    }

    override fun create(name: String, age: Int): Int {
        return userMapper!!.insert(name, age)

    }

//    override fun getByName(name: String): User {
//        return userMapper.findByName(name)
//    }

    override fun deleteByName(name: String): Int {
        return jdbcTemplate.update("delete from USER where NAME = ?", name)
    }

    override fun getAllUsers(): Int {
        return jdbcTemplate.queryForObject("select count(1) from USER", Int::class.java)!!
    }

    override fun deleteAllUsers(): Int {
        return jdbcTemplate.update("delete from USER")
    }
}